package com.minhua.jijiehao2.mainframe.circle.circleFragment.impl

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.LinearLayout
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.*
import com.minhua.jijiehao2.mainframe.circle.businessDetailActivity.impl.BusinessDetailActivity
import com.minhua.jijiehao2.mainframe.circle.businessListActivity.impl.BusinessListActivity
import com.minhua.jijiehao2.mainframe.circle.circleFragment.imp.ICircleView
import com.minhua.jijiehao2.mainframe.circle.entryActivity.impl.EntryActivity
import com.minhua.jijiehao2.mainframe.circle.moreTypeActivity.impl.MoreTypeActivity
import com.minhua.jijiehao2.mainframe.circle.productIdentifyActivity.impl.ProductIdentifyActivity
import com.minhua.jijiehao2.mainframe.common.webviewactivity.impl.WebActivity
import com.minhua.jijiehao2.utils.AppUtils
import com.minhua.jijiehao2.utils.NetImgBannerHolder
import kotlinx.android.synthetic.main.fragment_circle.*

class CircleFragment : BaseFragment<ICircleView, CirclePresenter>(), ICircleView {

    override fun getLayoutId(): Int = R.layout.fragment_circle

    override fun getPresenter(): CirclePresenter = CirclePresenter()

    companion object {
        fun newInstance(): CircleFragment {
            val fragment = CircleFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.tvService1 -> startActivity(Intent(activity, ProductIdentifyActivity::class.java))
            R.id.tvService2 -> BusinessListActivity.intentStart(activity, v.tag.toString())
            R.id.tvService3 -> BusinessListActivity.intentStart(activity, v.tag.toString())
            R.id.tvService4 -> BusinessListActivity.intentStart(activity, v.tag.toString())
            R.id.tvMore -> MoreTypeActivity.intentStart(activity, circleServiceTypes)
        }
    }

    private val listBanner = arrayListOf<String>()

    private val listMenu = arrayListOf<CircleServiceBean>()
    private var listMenuAdapter: BaseCommonAdapter<CircleServiceBean>? = null

    private val listRecommend = arrayListOf<BusinessBean>()
    private var listRecommendAdapter: BaseCommonAdapter<BusinessBean>? = null
    override fun initView() {
        refresh.setOnRefreshListener { mPresenter.getAllInfo() }

        ctTitle.viewHolder.setOnClickListener(R.id.ivRight, { startActivity(Intent(activity, EntryActivity::class.java)) })

        banner //设置指示器是否可见
                .setPointViewVisible(true)
                //设置自动切换（同时设置了切换时间间隔）
                .startTurning(2000)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(intArrayOf(R.mipmap.guide_point, R.mipmap.guide_point_cur))
                //设置指示器的方向（左、中、右）
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL).isManualPageable = true
        banner.setPages({ NetImgBannerHolder() }, listBanner)
        banner.setOnItemClickListener { position ->
            WebActivity.intentStart(activity, bannerPics[position].skip_url, "详情")
        }

        listMenuAdapter = object : BaseCommonAdapter<CircleServiceBean>(activity, R.layout.item_home_menu, listMenu) {
            override fun convert(holder: ViewHolder?, t: CircleServiceBean?) {
                holder?.itemView?.layoutParams = LinearLayout.LayoutParams(AppUtils.getScreenWidth(activity) / 4, LinearLayout.LayoutParams.MATCH_PARENT)
                holder?.setText(R.id.tvItemName, t?.type_name)
                Glide.with(activity).load(t?.type_img_path).into(holder?.getView(R.id.ivItemIcon))
                holder?.setOnClickListener(R.id.llItemView, { BusinessListActivity.intentStart(activity, t?.type.toString()) })
            }
        }
        rvMenu.isNestedScrollingEnabled = false
        rvMenu.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
        rvMenu.adapter = listMenuAdapter


        listRecommendAdapter = object : BaseCommonAdapter<BusinessBean>(activity, R.layout.item_circle_recommend, listRecommend) {
            override fun convert(holder: ViewHolder?, t: BusinessBean?) {
                Glide.with(activity).load(t?.logo_path).into(holder?.getView(R.id.ivIcon))
                holder?.setText(R.id.tvItemName, t?.proc_name)
                holder?.setText(R.id.tvItemMessage, t?.introduction)
                holder?.setText(R.id.tvItemType, t?.flag_msg)
                holder?.setOnClickListener(R.id.itemView, { BusinessDetailActivity.intentStart(activity, t!!) })
            }

        }
        rvRecommend.isNestedScrollingEnabled = false
        rvRecommend.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        rvRecommend.adapter = listRecommendAdapter

        tvService1.setOnClickListener(mOnClickListener)
        tvService2.setOnClickListener(mOnClickListener)
        tvService3.setOnClickListener(mOnClickListener)
        tvService4.setOnClickListener(mOnClickListener)
        tvMore.setOnClickListener(mOnClickListener)
    }

    override fun initData() {
        mPresenter.getAllInfo()
    }


    private val bannerPics = arrayListOf<CirclePicsBean>()
    private val circleServiceTypes = arrayListOf<CircleServiceTypeBean>()
    override fun onDataSuccess(circleBean: CircleBean) {
        refresh.isRefreshing = false

        bannerPics.clear()
        listBanner.clear()
        bannerPics.addAll(circleBean.bannerPics)
        bannerPics.forEach { listBanner.add(it.banner_url) }
        banner.notifyDataSetChanged()

        listMenu.clear()
        circleBean.serviceType.filterTo(listMenu) { it.big_type == 1 }
        listMenuAdapter?.notifyDataSetChanged()



        circleServiceTypes.addAll(circleBean.moreInterface)
        for (i in 0..3) {
            when (i) {
                0 -> {
                    tvService1Name.text = circleServiceTypes[i].type_name
                    tvService1.tag = circleServiceTypes[i].type
                    Glide.with(activity).load(circleServiceTypes[i].type_img_path).into(ivService1)
                }
                1 -> {
                    tvService2Name.text = circleServiceTypes[i].type_name
                    tvService2.tag = circleServiceTypes[i].type
                    Glide.with(activity).load(circleServiceTypes[i].type_img_path).into(ivService2)
                }
                2 -> {
                    tvService3Name.text = circleServiceTypes[i].type_name
                    tvService3.tag = circleServiceTypes[i].type
                    Glide.with(activity).load(circleServiceTypes[i].type_img_path).into(ivService3)
                }
                3 -> {
                    tvService4Name.text = circleServiceTypes[i].type_name
                    tvService4.tag = circleServiceTypes[i].type
                    Glide.with(activity).load(circleServiceTypes[i].type_img_path).into(ivService4)
                }
            }
        }

        if (circleBean.adPics.isNotEmpty()) {
            Glide.with(activity).load(circleBean.adPics[0].adUrl).into(ivBanner)
            ivBanner.setOnClickListener { WebActivity.intentStart(activity, circleBean.adPics[0].skip_url, "详情") }
        }
        listRecommend.clear()
        listRecommend.addAll(circleBean.recommendProd)
        listRecommendAdapter?.notifyDataSetChanged()
    }
}
